//
//  EvaluateViewController.m
//  GameBoss
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "EvaluateViewController.h"
#import "AFN_Get.h"
#import "FocusModel.h"
#import "ArticleListModel.h"
#import "MoreCell.h"
#import "PictureScrollViewCell.h"
#import "UIImageView+WebCache.h"
#import "DataViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface EvaluateViewController ()

// 装focus的model数组
@property(nonatomic, retain)NSMutableArray *arrScroller;
// 装articleList的数组
@property(nonatomic, retain)NSMutableArray *arrArticleList;
// tableview
@property(nonatomic, retain)UITableView *tableView;

// 取出model中的图片url 放入数组
@property(nonatomic, retain)NSMutableArray *pictureArr;

// 把model多加2个然后放入数组来传值
@property(nonatomic, retain)NSMutableArray *modelArr;

@end

@implementation EvaluateViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrScroller = [NSMutableArray array];
        self.arrArticleList = [NSMutableArray array];
        self.pictureArr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
    }
    
    return self;
}

- (void)leftBuClick
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBuClick)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
#pragma --没有添加加载数据方法
    [self addHeaderNew];
#pragma mark 夜间判断
    //    if ([NightMode shareNightMode].isNight) {
    //        [[NightMode shareNightMode] loadNightModeInfoFormDisk];
    //    }
    [self loadDataFocus];

}
#pragma mark 刷新
- (void)addHeaderNew
{
    __unsafe_unretained typeof (self)vc= self;
    [self.tableView addHeaderWithCallback:^{
        [self.modelArr removeAllObjects];
//        [self.arrArticleList removeAllObjects];
        [self.arrScroller removeAllObjects];
        [vc loadData];
    }];
    [self.tableView headerBeginRefreshing];
}
#pragma mark 解析数据
- (void)loadData
{
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager GET:@"http://mrobot.pcgames.com.cn/v3/cms/channels/3" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableArray *articleListArr =responseObject[@"articleList"];
//        for (NSInteger i = 0; i < articleListArr.count; i ++) {
//            ArticleListModel *model = [[ArticleListModel alloc] init];
//            NSMutableDictionary *articleListDic = articleListArr[i];
//            [model setValuesForKeysWithDictionary:articleListDic];
//            [self.arrArticleList addObject:model];
//            [model release];
//        }
//        [self.modelArr addObject:[self.arrScroller lastObject]];
//        for (int i = 0; i < self.arrScroller.count; i++) {
//            [self.modelArr addObject:self.arrScroller[i]];
//        }
//        [self.modelArr addObject:[self.arrScroller firstObject]];
//        NSMutableArray *focusArr = responseObject[@"focus"];
//        for (NSInteger i = 0; i < focusArr.count; i++) {
//            FocusModel *model = [[FocusModel alloc] init];
//            NSMutableDictionary *focusDic = focusArr[i];
//            [model setValuesForKeysWithDictionary:focusDic];
//            [self.arrScroller addObject:model];
//            [model release];
//        }
//        [self addHeaderNew];
//        [self.tableView reloadData];
//        [self.tableView headerEndRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@", error);
//    }];

    // 解析focus
    [AFN_Get netWorkingTool:@"http://mrobot.pcgames.com.cn/v3/cms/channels/3" block:^(id responseObject) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = responseObject;
//        NSArray *arrFocus = [NSArray array];
//        arrFocus = dic[@"focus"];
//        for (NSDictionary *temp in arrFocus) {
//            FocusModel *model = [[FocusModel alloc] init];
//            [model setValuesForKeysWithDictionary:temp];
//            [self.arrScroller addObject:model];
//            [model release];
//        }
//        [self.modelArr addObject:[self.arrScroller lastObject]];
//        for (int i = 0; i < self.arrScroller.count; i++) {
//            [self.modelArr addObject:self.arrScroller[i]];
//        }
//        [self.modelArr addObject:[self.arrScroller firstObject]];
        // 解析articleList
        NSArray *arrArticleList = [NSArray array];
        arrArticleList = dic[@"articleList"];
        for (NSDictionary *temp in arrArticleList) {
            ArticleListModel *model = [[ArticleListModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            [self.arrArticleList addObject:model];
            [model release];
        }
//        [self addHeaderNew];
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
    }];
}

- (void)loadDataFocus
{
    [AFN_Get netWorkingTool:@"http://mrobot.pcgames.com.cn/v3/cms/channels/3" block:^(id responseObject) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = responseObject;
        NSArray *arrFocus = [NSArray array];
        arrFocus = dic[@"focus"];
        for (NSDictionary *temp in arrFocus) {
            FocusModel *model = [[FocusModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            [self.arrScroller addObject:model];
            [model release];
        }
        [self.modelArr addObject:[self.arrScroller lastObject]];
        for (int i = 0; i < self.arrScroller.count; i++) {
            [self.modelArr addObject:self.arrScroller[i]];
        }
        [self.modelArr addObject:[self.arrScroller firstObject]];
        [self.tableView reloadData];
//        [self.tableView headerEndRefreshing];
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.arrArticleList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *reuse = @"reuse1";
        PictureScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[PictureScrollViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        for (int i = 0; i < self.modelArr.count; i++) {
            [cell.pictureArr addObject:self.modelArr[i]];
        }
        // cell中的model传值, 第四步(评测)
        cell.targetEvaluate = self;
        return cell;
    }
    else{
        static NSString *reuse = @"reuse2";
        MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[MoreCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        ArticleListModel *model = self.arrArticleList[indexPath.row];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.image]];
        cell.titleL.text = model.title;
        cell.pubDateL.text = model.pubDate;
        cell.imageVS.image = [UIImage imageNamed:@"iconfont-liaotian-2"];
        cell.cmtCountL.text =[NSString stringWithFormat:@"%ld", (long)model.cmtCount];
        return cell;
    }
}

// cell中的model传值, 第五步(评测)
- (void)didTapScrollViewWithEvlu:(FocusModel *)focusModel
{
    NSLog(@"%@", focusModel.title);
    DataViewController *dataView = [[DataViewController alloc] init];
    dataView.focus = focusModel;
    dataView.name = focusModel.title;
    [self.navigationController pushViewController:dataView animated:YES];
    [dataView release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200 * Screen.height / 667;
    }
    else{
        return 100 * Screen.height / 667;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataViewController *dataVC = [[DataViewController alloc] init];
    ArticleListModel *model = self.arrArticleList[indexPath.row];
    dataVC.title = model.title;
    dataVC.art = model;
    [self.navigationController pushViewController:dataVC animated:YES];
    [dataVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}

- (void)dealloc
{
    [_arrScroller release];
    [_arrArticleList release];
//    [_tableView release];
    [_pictureArr release];
    [super dealloc];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
